package interfaceConfig

import (
	"context"
	"encoding/json"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/interfaceconfig"
	"github.com/zeromicro/go-zero/core/logc"

	"github.com/zeromicro/go-zero/core/logx"
)

type InterfaceConfigInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 详情
func NewInterfaceConfigInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InterfaceConfigInfoLogic {
	return &InterfaceConfigInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *InterfaceConfigInfoLogic) InterfaceConfigInfo(req *types.Empty) (resp *types.InterfaceConfigInfoData, err error) {
	resp = &types.InterfaceConfigInfoData{}

	u, err := l.svcCtx.SettingService.InterfaceConfig.InterfaceConfigInfo(l.ctx, &interfaceconfig.InterfaceConfigInfoReq{MerchantId: 1})

	if err != nil {
		logc.Error(l.ctx, "InterfaceConfigInfo error:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	if u.Data.Id == 0 {
		return nil, code.NotData
	}

	phoneConfig := make([]types.PhoneConfigReq, 0)
	_ = json.Unmarshal([]byte(u.Data.PhoneConfig), &phoneConfig)
	resp = &types.InterfaceConfigInfoData{
		Id:              u.Data.Id,
		MerchantId:      u.Data.MerchantId,
		Logo:            u.Data.Logo,
		Logo1:           u.Data.Logo1,
		Icon:            u.Data.Icon,
		SiteName:        u.Data.SiteName,
		LanguageList:    u.Data.LanguageList,
		DefaultLanguage: u.Data.DefaultLanguage,
		Currency:        u.Data.Currency,
		NameShowType:    u.Data.NameShowType,
		PhoneConfig:     phoneConfig,
	}

	return resp, nil
}
